package it.tcm.TeacherClassMate.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Set;
import it.tcm.TeacherClassMate.beans.OrderMainBean;
import it.tcm.TeacherClassMate.beans.OrderRowBean;
import it.tcm.TeacherClassMate.facade.OrderMainManageBase;
import it.tcm.TeacherClassMate.facade.OrderRowManageBase;
import it.tcm.TeacherClassMate.form.SortablesForm;
import it.tcm.TeacherClassMate.util.OrderRowBeanComparator;
import it.tcm.TeacherClassMate.util.Util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;



public class OrderedFormulasAction extends Action {

	// --------------------------------------------------------- Instance Variables

	// --------------------------------------------------------- Methods


	private Logger logger = Logger.getLogger(this.getClass().getName());
	
	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * 
	 */

	@SuppressWarnings("unchecked")
	public ActionForward execute(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) 
	{		
		 logger.info("ORDERED FORMULAS");
		 SortablesForm sform = (SortablesForm)form;
		 String mode = sform.getMode();
		 OrderRowManageBase ormb = new OrderRowManageBase();
		 OrderMainManageBase ommb = new OrderMainManageBase();
		 OrderMainBean omb = null;
		 Set<OrderRowBean> set = null;
		 if (mode != null && mode.equals("DELETE")) {
			 String id = sform.getId();
			 // cancello la orderrow e ricarico la pagina
			 OrderRowBean ob = ormb.getOrderRowById(Integer.valueOf(id));
			 ormb.delete(ob);
			 omb = (OrderMainBean)request.getSession().getAttribute(Util.ORDER_MAIN_OBJECT);
			 // devo ricaricare omb !
			 omb = ommb.getOrderMainById(omb.getId());
			 request.getSession().setAttribute(Util.ORDER_MAIN_OBJECT,omb);
			 return mapping.findForward("orderformulas"); 
		 } else {
			 String order =  sform.getOrder();
			 logger.info("order " + sform.getOrder());
			 if (order != null && !order.equals("undefined")) {
				 // metto l'attributo position alle rows con order ListItem[]=3&listItem[]=2&.....
				 String[] listItems = order.split("&");
				 int i = 1;
				 for (String listItem : listItems)  {
					 String id = listItem.substring(listItem.indexOf('=')+1, listItem.length());
					 OrderRowBean ob = ormb.getOrderRowById(Integer.valueOf(id));
					 ob.setPosition(i);
					 ormb.save(ob);
					 i++;
				 }
				 omb = (OrderMainBean)request.getSession().getAttribute(Util.ORDER_MAIN_OBJECT);
				 // devo ricaricare omb !
				 omb = ommb.getOrderMainById(omb.getId());
				 set = omb.getOrderRows();			
			 } else {
				 omb = (OrderMainBean)request.getSession().getAttribute(Util.ORDER_MAIN_OBJECT);
				 set = omb.getOrderRows();				
				 int i = 1;
				 for (OrderRowBean ob : set) {
					 ob.setPosition(i);
					 ormb.save(ob);
					 i++;
				 }
				 // devo ricaricare omb !
				 omb = ommb.getOrderMainById(omb.getId());
				 set = omb.getOrderRows();	 				 
			 }
			 ArrayList<OrderRowBean> array  = new ArrayList<OrderRowBean>(set); 
			 Collections.sort(array,new OrderRowBeanComparator());
			 if(request.getParameter("punteggio")!=null){
				 String st[]=request.getParameterValues("punteggio");
				 for(int i=0;i<st.length;i++){
					 logger.info(st[i]);
			     }
				 // a seconda della posizione aggiungo il punteggio
				 int i = 0;
				 for (OrderRowBean ob: array) {
					 if (st[i] != null && !st[i].equals("")) {
						 Integer points = Integer.valueOf(st[i]);
						 ob.setPoints(points);
						 ormb.save(ob);
						 logger.info("ob position " + ob.getPosition() + " ob points " + ob.getPoints());
						 i++;
					 }
				 }
			 } 
			 // devo ricaricare ancora omb !
			 omb = ommb.getOrderMainById(omb.getId());
			 request.getSession().setAttribute(Util.ORDER_MAIN_OBJECT,omb);
		 }
		 return mapping.findForward("orderedformulas"); 
		 
	}
	
	
}